博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
12.2 在资料页中显示关注者
阅读量:4095 次
发布时间:2019-05-25

本文共 5185 字,大约阅读时间需要 17 分钟。

一. 修改|-app/templates/user.html

    我们想在用户资料页面增加几个栏目如下:

       

    对应的功能举例说明如下:

        如果已登录的用户A(有关注权限的话)访问别的用户B的页面:

            如果A未关注B, 显示一个Follow按钮, 点击关注B, 如果A已经关注B, 显示一个Unfollow按钮, 点击取消关注B;

            Followers列出B的关注者(粉丝)的数量,点击显示B的关注者的列表;

            Followed列出B的被关注者(偶像)的数量, 点击显示B的被关注者的列表;

            如果B关注了A, 后面显示一个Followes you标签。

修改代码如下:

{% if current_user.can(Permission.Follow) and current_user != user %} {% if not current_user.is_following(user) %} Follow {% else %} Unfollow {% endif %} {% endif %} Followers: {

{ user.followers.count() }} Followed: {
{ user.followed.count() }}
{% if current_user.is_authenticated and current_user != user and user.is_following(current_user) %} Follows you {% endif %}

二. 修改路由函数

1. main.follow

@main.route('/follow/
')@login_required@permission_required(Permission.FOLLOW)def follow(username): user = User.query.filter_by(username=username).first() if not user: flash('Invalid user.') return redirect(url_for('.index')) if current_user.is_following(user): flash('You are already following this user.') return redirect(url_for('.user', username=username)) current_user.follow(user) flash('You are now following %s' % username) return redirect(url_for('.user', username=username))

代码解释:

    之前我们说过, 只有已经登录(1), 具有关注权限(2)的用户A, 访问别的用户(3)B的页面时, 才会根据不同情况关注或者取关B;

    所以我们用if语句, 只有满足以上三个条件时页面才会显示关注和取关按钮, 同样我们用修饰器, 只有满足前两个条件时, 我们才能访问试图函数。 以防止未登录用户直接访问关注链接导致程序出错, 或者无关注权限用户直接访问链接关注用户。

2. main.unfollow

@main.route('/unfollow/
')@login_required@permission_required(Permission.FOLLOW)def unfollow(username): user = User.query.filter_by(username=username).first() if not user: flash('Invalid user.') return redirect(url_for('.index')) if not current_user.is_following(user): flash('You never follow this user.') return redirect(url_for('.user', username)) current_user.unfollow(user) flash('You have already unfollowed %s.' % username) return redirect(url_for('.user', username=username))

3. main.followers

    用户点击Followers链接时, 由本视图函数处理请求,返回参数username对应用户的关注者。

@main.route('/followers/
')def followers(username): user = User.query.filter_by(username=username).first() if not user: flash('Invalid user.') return redirect(url_for('.index')) page = request.args.get('page', 1, type=int) pagination = user.followers.paginate(page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'], error_out=False) follows = [ {'user': item.follower, 'timestamp': item.timstamp} for item in pagination.items ] return render_tempalte('followers.html', user=user, title="Followers of", follows=follows, endpoint=".followers", pagination=pagination)

代码解释:

    pagination:把所有的记录分成per_page页, 返回第page页的页对象, items属性得到第page页的所有记录。 page通过查询参数page获得, 默认为1.

    follows把遍历该页所有记录, 得到所有的关注者和关注时间戳。

4. main.followed_by

    用户点击Followed链接时, 由本视图函数处理请求,返回参数username对应用户的被关注者。

@main.route('/followed_by/
')def followed_by(username): user = User.query.filter_by(username=username).first() if not user: flash('Invalid user.') return redirect(url_for('.index')) page = request.args.get('page', 1, type=int) pagination = user.followed.paginate(page, per_page=current_app['FLASKY_FOLLOWERS_PER_PAGE'], error_out=False) followeds = [{'user': item.followed, 'timestamp': item.timestamp} for item in pagination.items] return render_template('followers.html', user=user, title="Followeds of", endpoint='.followed_by', pagination=pagination, followeds=followeds)

5.followers.html模板

    我们看main.followers视图函数最后的返回语句:

       

return render_tempalte('followers.html', user=user, title="Followers of", follows=follows, endpoint=".followers", pagination=pagination)

    我们返回的前两个参数, 供followers页面的标题使用, 第三个参数是该用户所有的关注者, 显示在一个table里, 最后两个参数供分页使用, 我们点击Followers按钮时想得到这样一个页面:

   

{% extends "base.html" %}{% import "_micros.html" as micros %}{% block title %}Flasky - {
{ title }}{
{ user.username }}{% endblock %}{% block page_content %}
{% for follow in follows%} {% if follow['user'] != user %}
{% endif %} {% endfor %}
User Since
{
{ follow['user'].username }}
{
{ moment(follow['timestamp']).format('L') }}
{% endblock %}

6. 补充

    followers.html页面里面的宏, 代码如下, 宏的**kwargs参数自动传递:

{% macro pagination_widget(pagination, endpoint) %}
  • «
  • {% for p in pagination.iter_pages() %} {% if p %} {% if p == pagination.page %}
  • {
    { p }}
  • {% else %}
  • {
    { p }}
  • {% endif %} {% else %}
  • {% endif %} {% endfor %}
  • »
{% endmacro %}

   

你可能感兴趣的文章
今天,Python信息量很大!
查看>>
Flash 已死,Deno 当立?
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
编程差的程序员,90%都是吃了数学的亏!骨灰级开发:方法不对,努力也白费...
查看>>
都无代码了,还要程序员吗?
查看>>
面试想拿 10K,HR 说我只配7k?
查看>>
副业过万的程序员都知道的网站有哪些
查看>>
那些人生“开挂”的程序员,都在干什么?
查看>>
影响科学圈的那些计算机代码
查看>>
乐视视频 App 图标改为“欠 122 亿”,网友:我在别家分红包,却在你家随份子!...
查看>>
乔布斯18岁求职信拍卖价22.24万美元,值吗?
查看>>
为何程序员总喜欢写技术博客,看完恍然大悟...
查看>>
假如计算机是中国人发明的,那代码应该这么写
查看>>
科技公司最爱的 50 款开源工具,你都用过吗?
查看>>
触目惊心:比特币到底消耗了多少能源?
查看>>
面试官:简历上敢写技术精通?那我就不客气了!
查看>>
如何判断一家互联网公司要倒闭了?
查看>>
想快速上手机器学习?来看下这个 GitHub 项目!
查看>>
GitHub 标星 3.6k,一本开源的深度学习中文教程!
查看>>
9 款你不能错过的 JSON 工具
查看>>